package com.nb.serv;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Servlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		BaseDAO dao = new BaseDAO();
		try {
			String sql = req.getParameter("sql");
			if(sql==null||sql.length()==0||sql.equals("null")){
				return;
			}
			System.out.println("executeQuery:"+sql);
			String p1 = req.getParameter("pn");
			String p2 = req.getParameter("ps");
			int pn = 1,ps = 20;
			try{
				pn = Integer.valueOf(p1);
			}catch (Exception e) {
				// TODO: handle exception
			}
			
			try{
				ps = Integer.valueOf(p2);
			}catch (Exception e) {
				// TODO: handle exception
			}
			
			JSONObject res = dao.executeQuery(sql,pn,ps);
			resp.setContentType("text/html;charset=UTF-8");
			resp.setCharacterEncoding("UTF-8");
			resp.getWriter().println(res.toString());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
